package com.pp.demo.module.api.controller;


import com.pp.demo.module.api.entity.SeckillOrder;
import com.pp.demo.module.api.entity.SeckillActivity;
import com.pp.demo.module.api.service.SeckillService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author pzh
 * @since 2025-07-14 09:45:25
 */
@Tag(name = "秒杀活动")
@RestController
@RequestMapping("/api/seckill")
public class SeckillController {

    @Autowired
    private SeckillService seckillService;

    @Operation(summary = "列出所有有效活动")
    @GetMapping("/activities")
    public List<SeckillActivity> getActiveSeckillActivities() {
        return seckillService.getActiveSeckillActivities();
    }

    @Operation(summary = "检查活动库存")
    @GetMapping("/checkStock/{activityId}")
    public boolean checkStock(@PathVariable Long activityId) {
        return seckillService.checkStock(activityId);
    }

    @Operation(summary = "秒杀")
    @PostMapping("/process/{userId}/{activityId}")
    public String processSeckill(@PathVariable Long userId, @PathVariable Long activityId) {
        SeckillOrder seckillOrder = seckillService.processSeckill(userId, activityId);
        if (seckillOrder != null) {
            return "秒杀成功，订单ID：" + seckillOrder.getId();
        } else {
            return "秒杀失败，库存不足";
        }
    }

    @Operation(summary = "获取秒杀结果")
    @GetMapping("/result/{orderId}")
    public String getSeckillResult(@PathVariable Long orderId) {
        return seckillService.getSeckillResult(orderId);
    }
}
